package io.renren.modules.shipping_area.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.SerializeUtil;
import io.renren.modules.area_region.entity.Trans;
import io.renren.modules.area_region.entity.V9AreaRegionEntity;
import io.renren.modules.area_region.service.V9AreaRegionService;
import io.renren.modules.region.entity.V9RegionEntity;
import io.renren.modules.region.service.V9RegionService;
import io.renren.modules.shipping.entity.V9ShippingEntity;
import io.renren.modules.shipping.service.V9ShippingService;
import io.renren.modules.shipping_area.entity.V9ShippingAreaEntity;
import io.renren.modules.shipping_area.service.V9ShippingAreaService;
import lombok.val;
import net.sf.json.JSONObject;
import oracle.net.aso.e;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * 地区列表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-09-19 16:50:17
 */
@RestController
@RequestMapping("shipping_area/v9shippingarea")
public class V9ShippingAreaController {
	@Autowired
	private V9ShippingAreaService v9ShippingAreaService;
	@Autowired
	private V9ShippingService v9ShippingService;
	@Autowired
	private V9RegionService v9RegionService;
	@Autowired
	private V9AreaRegionService v9AreaRegionService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("shipping_area:v9shippingarea:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = v9ShippingAreaService.queryPage(params);
		List<V9ShippingAreaEntity> list = (List<V9ShippingAreaEntity>) page.getList();
		for (V9ShippingAreaEntity v9ShippingAreaEntity : list) {
			Integer shippingId = v9ShippingAreaEntity.getShippingId();
			V9ShippingEntity byId = v9ShippingService.getById(shippingId);
			if (byId != null) {
				v9ShippingAreaEntity.setShippingName(byId.getShippingName());
			} else {
				v9ShippingAreaEntity.setShippingName("");
			}
			Integer shippingAreaId=v9ShippingAreaEntity.getShippingAreaId();
			List<V9AreaRegionEntity>selcountries=v9AreaRegionService.list(new QueryWrapper<V9AreaRegionEntity>().eq("shipping_area_id", shippingAreaId));
			String [] nArr=new String[selcountries.size()];
			for (V9AreaRegionEntity v9AreaRegionEntity : selcountries) {
				Integer regionId=v9AreaRegionEntity.getRegionId();
				V9RegionEntity one = v9RegionService.getOne(new QueryWrapper<V9RegionEntity>().eq("region_id", regionId));
				 
			}
			for (int i = 0; i < selcountries.size(); i++) {
				Integer regionId=selcountries.get(i).getRegionId();
				V9RegionEntity one = v9RegionService.getOne(new QueryWrapper<V9RegionEntity>().eq("region_id", regionId));
				nArr[i]=one.getRegionName(); 
			}
			v9ShippingAreaEntity.setAreaCountry(StringUtils.join(nArr, ","));
			
		}
		List<V9ShippingEntity> v9ShippingEntitiesList = v9ShippingService
				.list(new QueryWrapper<V9ShippingEntity>().orderByDesc("listorder"));
		List<V9RegionEntity> rList = v9RegionService.list();
		return R.ok().put("page", page).put("shipping", v9ShippingEntitiesList).put("countries", rList);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{shippingAreaId}")
	@RequiresPermissions("shipping_area:v9shippingarea:info")
	public R info(@PathVariable("shippingAreaId") Integer shippingAreaId) {
		V9ShippingAreaEntity v9ShippingArea = v9ShippingAreaService.getById(shippingAreaId);
		List<V9ShippingEntity> v9ShippingEntitiesList = v9ShippingService
				.list(new QueryWrapper<V9ShippingEntity>().orderByDesc("listorder"));
		List<V9RegionEntity> rList = v9RegionService.list();
		Integer shiAreaId=v9ShippingArea.getShippingAreaId();
		List<V9AreaRegionEntity>selcountries=v9AreaRegionService.list(new QueryWrapper<V9AreaRegionEntity>().eq("shipping_area_id", shiAreaId));
		String configure=v9ShippingArea.getConfigure();
		Trans unserialize = Trans.getUnserialize(configure);
		return R.ok().put("selcountries", selcountries).put("configure", unserialize).put("v9ShippingArea", v9ShippingArea).put("shipping", v9ShippingEntitiesList).put("countries", rList);
	}
	@RequestMapping("/getshipcountry")
	public R getshipcountry() {
		List<V9ShippingEntity> v9ShippingEntitiesList = v9ShippingService
				.list(new QueryWrapper<V9ShippingEntity>().orderByDesc("listorder"));
		List<V9RegionEntity> rList = v9RegionService.list();
		return R.ok().put("shipping", v9ShippingEntitiesList).put("countries", rList);
	}

	/**
	 * 保存
	 * @throws IOException 
	 */
	@RequestMapping("/save")
	@RequiresPermissions("shipping_area:v9shippingarea:save")
	public R save(HttpServletRequest req) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
		StringBuffer sb = new StringBuffer();
		String s = null;
		while ((s = br.readLine()) != null) {
			sb.append(s);
		}
		JSONObject jsonObject = JSONObject.fromObject(sb.toString());
		//String shippingAreaId = jsonObject.getString("shippingAreaId");
		String shippingAreaName = jsonObject.getString("shippingAreaName");
		String shippingId = jsonObject.getString("shippingId");
		String region_id = jsonObject.getString("region_id");
		String start_weight = jsonObject.getString("start_weight");
		String top_weight = jsonObject.getString("top_weight");
		String base_weight = jsonObject.getString("base_weight");
		String step_weight = jsonObject.getString("step_weight");
		String base_fee = jsonObject.getString("base_fee");
		String step_fee = jsonObject.getString("step_fee");
		String discount = jsonObject.getString("discount");
		String time = jsonObject.getString("time");
		String declaration = jsonObject.getString("declaration");
		String volueme = jsonObject.getString("volueme");
		Trans trans=new Trans();
		trans.setBase_fee(base_fee);
		trans.setBase_weight(base_weight);
		trans.setDeclaration(declaration);
		trans.setDiscount(discount);
		trans.setStart_weight(start_weight);
		trans.setStep_fee(step_fee);
		trans.setStep_weight(step_weight);
		trans.setTime(time);
		trans.setTop_weight(top_weight);
		trans.setVolueme(volueme);
		Map<String, Object> bean2Map = SerializeUtil.bean2Map(trans); 
		String configure = SerializeUtil.serialize2(bean2Map);
		V9ShippingAreaEntity vAreaEntity=new V9ShippingAreaEntity();
		vAreaEntity.setConfigure(configure);
		vAreaEntity.setShippingAreaName(shippingAreaName);
		vAreaEntity.setShippingId(Integer.parseInt(shippingId));
		Integer shippingAreaId=v9ShippingAreaService.insertGetId(vAreaEntity);
		V9AreaRegionEntity entity=new V9AreaRegionEntity();
		entity.setRegionId(Integer.parseInt(region_id));
		entity.setShippingAreaId(shippingAreaId);
		System.out.println("----------");
		System.out.println(shippingAreaId);
		System.out.println(region_id);
		System.out.println("----------");
		v9AreaRegionService.saveOrUpdate(entity);
		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("shipping_area:v9shippingarea:update")
	public R update(@RequestBody V9ShippingAreaEntity v9ShippingArea) {
		v9ShippingAreaService.updateById(v9ShippingArea);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("shipping_area:v9shippingarea:delete")
	public R delete(@RequestBody Integer[] shippingAreaIds) {
		v9ShippingAreaService.removeByIds(Arrays.asList(shippingAreaIds));

		return R.ok();
	}

	/**
	 * 地区名称查询
	 */
	@RequestMapping("/queryPage")
	public R queryPage(@RequestParam Map<String, Object> params) {
		v9ShippingAreaService.queryPage(params);
		return R.ok();
	}

}